import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from '@/utils/auth'

const Login = () => import('@/views/login')
const Layout = () => import('@/views/Layout.vue')
const Home = () => import('@/views/home')
const Article = () => import('@/views/article')
const Publish = () => import('@/views/publish')

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      children: [{ path: '/', component: Home }, { path: '/article', component: Article },
        { path: '/publish', component: Publish }]
    }
  ]
})
// 导航守卫
router.beforeEach((to, from, next) => {
  const token = auth.getToken()
  // 如果未登录,访问的不是登陆页面,拦截到登录页面
  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    // 其他情况放行
    next()
  }
})

export default router
